home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / Stl / h / string < prev   
Text File  |  2003-02-14  |  14KB  |  343 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38.  
  39. #ifndef stl_string_h
  40. #define stl_string_h
  41.  
  42. #include "bool.h"
  43. #include <string.h>
  44. class istream;
  45. class ostream;
  46.  
  47. /* NOTE
  48.   ******** compiler will not create functions inline unless declared prior to use *******
  49. */
  50.  
  51. class string;
  52.  
  53. string operator+(const string& lhs, const string& rhs);
  54. string operator+(const char* lhs, const string& rhs);
  55. string operator+(char lhs, const string& rhs);
  56. string operator+(const string& lhs, const char* rhs);
  57. string operator+(const string& lhs, char rhs);
  58.  
  59. bool operator==(const string& lhs, const string& rhs);
  60. bool operator==(const char* lhs, const string& rhs);
  61. bool operator==(const string& lhs, const char* rhs);
  62.  
  63. bool operator!=(const string& lhs, const string& rhs);
  64. bool operator!=(const char* lhs, const string& rhs);
  65. bool operator!=(const string& lhs, const char* rhs);
  66.  
  67. bool operator< (const string& lhs, const string& rhs);
  68. bool operator< (const string& lhs, const char* rhs);
  69. bool operator< (const char* lhs, const string& rhs);
  70.  
  71. bool operator> (const string& lhs, const string& rhs);
  72. bool operator> (const string& lhs, const char* rhs);
  73. bool operator> (const char* lhs, const string& rhs);
  74.  
  75. bool operator<=(const string& lhs, const string& rhs);
  76. bool operator<=(const string& lhs, const char* rhs);
  77. bool operator<=(const char* lhs, const string& rhs);
  78.  
  79. bool operator>=(const string& lhs, const string& rhs);
  80. bool operator>=(const string& lhs, const char* rhs);
  81. bool operator>=(const char* lhs, const string& rhs);
  82.  
  83. void swap(string& lhs, string& rhs);
  84.  
  85. istream& operator>>(istream& is, string& str);
  86. ostream& operator<<(ostream& os, const string& str);
  87. istream& getline(istream& is, string& str, char delim='\n');
  88.  
  89.  
  90. class string
  91. {
  92.   public:
  93.  
  94.     typedef unsigned int  size_type;
  95.     typedef char*         iterator;
  96.     typedef const char*   const_iterator;
  97.  
  98.     enum {npos=-1}; // *largest* size_type
  99.  
  100.     // construct/copy/destroy
  101.     string();
  102.     string(const string& str,size_type pos=0,size_type n=npos);
  103.     string(const char* s,size_type n);
  104.     string(const char* s);
  105.     string(size_type n,char c);
  106.     string(iterator begin,iterator end);
  107.  
  108.     ~string();
  109.  
  110.     string& operator=(const string& str);
  111.     string& operator=(const char* s);
  112.     string& operator=(char c);
  113.     
  114.   // iterators
  115.     iterator       begin();
  116.     const_iterator begin() const;
  117.     iterator       end();
  118.     const_iterator end() const;
  119.  
  120.     iterator       rbegin();
  121.     const_iterator rbegin() const;
  122.     iterator       rend();
  123.     const_iterator rend() const;
  124.        
  125.   // capacity
  126.     size_type size() const;
  127.     size_type length() const;
  128.     size_type max_size() const;
  129.     void resize(size_type n, char c);
  130.     void resize(size_type n);
  131.     size_type capacity() const;
  132.     void reserve(size_type res_arg = 0);
  133.     void clear();
  134.     bool empty() const;
  135.  
  136.   // element access:
  137.     const char& operator[](size_type pos) const;
  138.     char&       operator[](size_type pos);
  139.     const char& at(size_type n) const;
  140.     char&       at(size_type n);
  141.  
  142.   // _lib.string.modifiers_ modifiers:
  143.     string& operator+=(const string& str);
  144.     string& operator+=(const char* s);
  145.     string& operator+=(char c);
  146.  
  147.     string& append(const string& str);
  148.     string& append(const string& str, size_type pos,size_type n=npos);
  149.     string& append(const char* s, size_type n);
  150.     string& append(const char* s);
  151.     string& append(size_type n, char c);
  152.     string& append(iterator first, iterator last);
  153.  
  154.     string& assign(const string&);
  155.     string& assign(const string& str, size_type pos,size_type n=npos);
  156.     string& assign(const char* s, size_type n);
  157.     string& assign(const char* s);
  158.     string& assign(size_type n, char c);
  159.     string& assign(iterator first, iterator last);
  160.  
  161.     string&  insert(size_type pos1, const string& str);
  162.     string&  insert(size_type pos1, const string& str,size_type pos2, size_type n=npos);
  163.     string&  insert(size_type pos, const char* s, size_type n);
  164.     string&  insert(size_type pos, const char* s);
  165.     string&  insert(size_type pos, size_type n, char c);
  166.     iterator insert(iterator i, char c = 0);
  167.     void     insert(iterator i, size_type n, char c);
  168.     void     insert(iterator i, iterator first, iterator last);
  169.  
  170.     string&   erase(size_type pos = 0, size_type n = npos);
  171.     iterator  erase(iterator position);
  172.     iterator  erase(iterator first, iterator last);
  173.  
  174.     string& replace(size_type pos1, size_type n1, const string& str);
  175.     string& replace(size_type pos1, size_type n1, const string& str,
  176.                           size_type pos2, size_type n2);
  177.     string& replace(size_type pos, size_type n1, const char* s,size_type n2);
  178.     string& replace(size_type pos, size_type n1, const char* s);
  179.     string& replace(size_type pos, size_type n1, size_type n2, char c);
  180.     string& replace(iterator i1, iterator i2, const string& str);
  181.     string& replace(iterator i1, iterator i2, const char* s, size_type n);
  182.     string& replace(iterator i1, iterator i2, const char* s);
  183.     string& replace(iterator i1, iterator i2, size_type n, char c);
  184.     string& replace(iterator i1, iterator i2, iterator j1, iterator j2);
  185.  
  186.     size_type copy(char* s, size_type n, size_type pos = 0) const;
  187.  
  188.     void swap(string&);
  189.  
  190.   // string operations:
  191.     const char* c_str() const;  // explicit
  192.     const char* data() const;
  193.  //   allocator_type get_allocator() const;
  194.  
  195.     size_type find (const string& str, size_type pos = 0) const;
  196.     size_type find (const char* s, size_type pos, size_type n) const;
  197.     size_type find (const char* s, size_type pos = 0) const;
  198.     size_type find (char c, size_type pos = 0) const;
  199.  
  200.     size_type rfind(const string& str, size_type pos = npos) const;
  201.     size_type rfind(const char* s, size_type pos, size_type n) const;
  202.     size_type rfind(const char* s, size_type pos = npos) const;
  203.     size_type rfind(char c, size_type pos = npos) const;
  204.  
  205.     size_type find_first_of(const string& str, size_type pos = 0) const;
  206.     size_type find_first_of(const char* s, size_type pos, size_type n) const;
  207.     size_type find_first_of(const char* s, size_type pos = 0) const;
  208.     size_type find_first_of(char c, size_type pos = 0) const;
  209.  
  210.     size_type find_last_of (const string& str,size_type pos = npos) const;
  211.     size_type find_last_of (const char* s, size_type pos, size_type n) const;
  212.     size_type find_last_of (const char* s, size_type pos = npos) const;
  213.     size_type find_last_of (char c, size_type pos = npos) const;
  214.  
  215.     size_type find_first_not_of(const string& str, size_type pos = 0) const;
  216.     size_type find_first_not_of(const char* s, size_type pos, size_type n) const;
  217.     size_type find_first_not_of(const char* s, size_type pos = 0) const;
  218.     size_type find_first_not_of(char c, size_type pos = 0) const;
  219.  
  220.     size_type find_last_not_of (const string& str, size_type pos = npos) const;
  221.     size_type find_last_not_of (const char* s, size_type pos, size_type n) const;
  222.     size_type find_last_not_of (const char* s, size_type pos = npos) const;
  223.     size_type find_last_not_of (char c, size_type pos = npos) const;
  224.  
  225.     string substr(size_type pos = 0, size_type n = npos) const;
  226.  
  227.     int compare(const string& str) const;
  228.     int compare(size_type pos1, size_type n1, const string& str) const;
  229.     int compare(size_type pos1, size_type n1, const string& str,
  230.                 size_type pos2, size_type n2) const;
  231.     int compare(const char* s) const;
  232.     int compare(size_type pos1, size_type n1, const char* s, size_type n2 = npos) const;
  233.  
  234.     static string add(const char *s1,int len1,const char *s2,int len2);
  235.   private:
  236.  
  237.     struct InternalString
  238.     {
  239.         struct Hdr
  240.         {
  241.             size_type size;
  242.             size_type capacity;
  243.             size_type count;
  244.             Hdr() :
  245.               size(0),
  246.               capacity(0),
  247.               count(2) {}
  248.         }hdr;
  249.       char data[1];
  250.       InternalString() {data[0]=0;}
  251.     } *p;
  252.     
  253.     static InternalString null_string;
  254.     InternalString* allocate(size_type n);
  255.     void deallocate(InternalString*);
  256.     bool reserve_now(size_type n);
  257.     bool midextend(size_type pos,int by);
  258.  
  259. };
  260.  
  261. inline string::size_type string::size() const        {return p->hdr.size;}
  262. inline string::size_type string::length() const      {return size();}
  263. inline string::size_type string::capacity() const    {return p->hdr.capacity;}
  264. inline string::size_type string::max_size() const    {return 512;}
  265. inline void              string::resize(size_type n) {resize(n,' ');}
  266. inline void              string::clear()             {deallocate(p);p=&null_string;}
  267. inline bool              string::empty() const       {return size()==0;}
  268.  
  269.  
  270. inline const char*       string::c_str() const    {p->data[size()]=0;return p->data;}
  271. inline const char*       string::data() const     {return p->data;}
  272.  
  273.  
  274. inline string& string::operator=(const string& str) {return assign(str);}
  275. inline string& string::operator=(const char* s)     {return assign(s);}
  276. inline string& string::operator=(char c)            {return assign(1,c);}
  277.  
  278. inline string::const_iterator string::begin() const   {return p->data;}
  279. inline string::iterator       string::begin()         {reserve_now(size());return p->data;}
  280. inline string::const_iterator string::end() const     {return begin()+size();}
  281. inline string::iterator       string::end()           {return begin()+size();}
  282. inline string::const_iterator string::rbegin() const  {return begin()+size();}
  283. inline string::iterator       string::rbegin()        {return begin()+size();}
  284. inline string::const_iterator string::rend() const    {return begin();}
  285. inline string::iterator       string::rend()          {return begin();}
  286.  
  287.  
  288. inline const char& string::at(size_type n) const           {return *(begin()+n);}
  289. inline char&       string::at(size_type n)                 {return *(begin()+n);}
  290. inline const char& string::operator[](size_type pos) const {return at(pos);}
  291. inline char&       string::operator[](size_type pos)       {return at(pos);}
  292.  
  293. inline string& string::operator+=(const string& str) {return append(str);}
  294. inline string& string::operator+=(const char* s)     {return append(s);}
  295. inline string& string::operator+=(char c)            {return append(1,c);}
  296.  
  297. inline string& string::assign(iterator first, iterator last)
  298.                                          {return assign(first,last-first);}
  299.  
  300. inline void string::swap(string& str)  {InternalString *is=p;p=str.p;str.p=is;}
  301.  
  302. inline string string::substr(size_type pos, size_type n) const
  303.                                            {return string(*this,pos,n);}
  304.  
  305. //******************************************************************
  306.  
  307. inline string operator+(const string& lhs, const string& rhs)
  308.       {return string::add(lhs.data(),lhs.size(),rhs.data(),rhs.size());}
  309. inline string operator+(const char* lhs, const string& rhs)
  310.       {return string::add(lhs,strlen(lhs),rhs.data(),rhs.size());}
  311. inline string operator+(const string& lhs, const char* rhs)
  312.       {return string::add(lhs.data(),lhs.size(),rhs,strlen(rhs));}
  313.  
  314. inline bool operator==(const string& lhs, const string& rhs) {return lhs.compare(rhs)==0;}
  315. inline bool operator==(const char* lhs, const string& rhs)   {return rhs.compare(lhs)==0;}
  316. inline bool operator==(const string& lhs, const char* rhs)   {return lhs.compare(rhs)==0;}
  317.  
  318. inline bool operator!=(const string& lhs, const string& rhs)  {return lhs.compare(rhs)!=0;}
  319. inline bool operator!=(const char* lhs, const string& rhs)    {return rhs.compare(lhs)!=0;}
  320. inline bool operator!=(const string& lhs, const char* rhs)    {return lhs.compare(rhs)!=0;}
  321.  
  322. inline bool operator< (const string& lhs, const string& rhs)  {return lhs.compare(rhs)<0;}
  323. inline bool operator< (const string& lhs, const char* rhs)    {return lhs.compare(rhs)<0;}
  324. inline bool operator< (const char* lhs, const string& rhs)    {return rhs.compare(lhs)>=0;}
  325.  
  326. inline bool operator> (const string& lhs, const string& rhs)  {return lhs.compare(rhs)>0;}
  327. inline bool operator> (const string& lhs, const char* rhs)    {return lhs.compare(rhs)>0;}
  328. inline bool operator> (const char* lhs, const string& rhs)    {return rhs.compare(lhs)<=0;}
  329.  
  330. inline bool operator<=(const string& lhs, const string& rhs) {return lhs.compare(rhs)<=0;}
  331. inline bool operator<=(const string& lhs, const char* rhs)   {return lhs.compare(rhs)<=0;}
  332. inline bool operator<=(const char* lhs, const string& rhs)   {return rhs.compare(lhs)>0;}
  333.  
  334. inline bool operator>=(const string& lhs, const string& rhs) {return lhs.compare(rhs)>=0;}
  335. inline bool operator>=(const string& lhs, const char* rhs)   {return lhs.compare(rhs)>=0;}
  336. inline bool operator>=(const char* lhs, const string& rhs)   {return rhs.compare(lhs)<0;}
  337.  
  338.  
  339. inline void swap(string& lhs, string& rhs) {lhs.swap(rhs);}
  340.  
  341. #endif
  342.  
  343.